﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using MM.DBUtility;

namespace MobileManager
{
    public partial class UserCusManager : System.Web.UI.Page
    {
        private MM.Model.T_Users userModel;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurUser"] == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }
            else
            {
                userModel = (MM.Model.T_Users)Session["CurUser"];
            }
            if (!IsPostBack)
            {
                TVusersBind();
                TVcustomersBind("", "");
                btnSave.Visible = false;
            }
        }
        private void TVusersBind()
        {
            TreeNode nodeRoot = new TreeNode();
            nodeRoot.Text = "客户信息";
            nodeRoot.Expanded = true;
            nodeRoot.SelectAction = TreeNodeSelectAction.None;

            DataSet ds = DbHelperSql.Query("SELECT UserID,UserName FROM T_Users WHERE RoleID=3 ");
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    TreeNode nodeChild = new TreeNode();

                    if (ViewState["SelectNodeVlaue"] != null && row["UserID"].ToString().Equals(ViewState["SelectNodeVlaue"].ToString()))
                        nodeChild.Text = "<span class=\"TreeNodeInfoSelected\">" + row["UserName"].ToString() + "(" + row["UserID"].ToString() + ")" + "</span>";
                    else
                        nodeChild.Text = "<span class=\"TreeNodeInfo\">" + row["UserName"].ToString() + "(" + row["UserID"].ToString() + ")" + "</span>";
                    nodeChild.Value = row["UserID"].ToString();
                    nodeChild.ToolTip = "点击选择客户编号";
                    nodeChild.PopulateOnDemand = false;
                    nodeChild.SelectAction = TreeNodeSelectAction.Select;
                    nodeRoot.ChildNodes.Add(nodeChild);
                }
            }

            TVusers.Nodes.Clear();
            TVusers.ExpandDepth = 1;
            TVusers.ShowExpandCollapse = true;
            TVusers.ShowLines = false;
            TVusers.Nodes.Add(nodeRoot);
        }
        private void TVcustomersBind(string strText, string UserID)
        {
            TreeNode nodeRoot = new TreeNode();
            nodeRoot.Text = strText + "客户编号";
            nodeRoot.Expanded = true;
            nodeRoot.SelectAction = TreeNodeSelectAction.None;

            DataSet ds = DbHelperSql.Query("SELECT CNO,CName FROM T_Customers ORDER BY CreateTime DESC ");
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    TreeNode nodeChild = new TreeNode();
                    nodeChild.Text = row["CNO"].ToString() + "-" + row["CName"].ToString();
                    nodeChild.Value = row["CNO"].ToString();
                    nodeChild.PopulateOnDemand = false;
                    nodeChild.SelectAction = TreeNodeSelectAction.None;

                    if (CheckUserCus(UserID, row["CNO"].ToString()))
                        nodeChild.Checked = true;
                    else
                        nodeChild.Checked = false;
                    nodeRoot.ChildNodes.Add(nodeChild);
                }
            }

            TVcustomers.Nodes.Clear();
            TVcustomers.ExpandDepth = 1;
            TVcustomers.ShowExpandCollapse = true;
            TVcustomers.ShowLines = false;
            TVcustomers.Nodes.Add(nodeRoot);
        }

        protected void TVusers_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (TVusers.SelectedNode != null)
            {
                TVcustomersBind(TVusers.SelectedNode.Text, TVusers.SelectedNode.Value);
                ViewState["SelectNodeVlaue"] = TVusers.SelectedNode.Value;
                TVusersBind();
                btnSave.Visible = true;
            }
        }
        private bool CheckUserCus(string UserID, string CNO)
        {
            return DbHelperSql.Exists(string.Format("SELECT ID FROM T_User_Customer WHERE UserID='{0}' AND CNO='{1}'", UserID, CNO));
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (ViewState["SelectNodeVlaue"] == null)
                {
                    MM.Common.Common.PageStartAlert(this.Page, "请选择客户！");
                    return;
                }
                if (string.IsNullOrEmpty(ViewState["SelectNodeVlaue"].ToString()))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "请选择客户！");
                    return;
                }
                ArrayList SQLlist = new ArrayList();
                SQLlist.Add(string.Format("DELETE FROM T_User_Customer WHERE UserID='{0}'", ViewState["SelectNodeVlaue"].ToString()));
                foreach (TreeNode node in TVcustomers.Nodes[0].ChildNodes)
                {
                    if (node.Checked)
                        SQLlist.Add(string.Format("INSERT INTO T_User_Customer(UserID,CNO) VALUES('{0}','{1}')", ViewState["SelectNodeVlaue"].ToString(), node.Value));
                }

                DbHelperSql.ExecuteSqlTran(SQLlist);
                MM.Common.Common.PageStartAlert(this.Page, "保存成功！");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
